package com.mission.schedule.my160920.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.applcation.App;
import com.mission.schedule.my160920.SwipeLayout;
import com.mission.schedule.my160920.bean.CommonReplyBean;

import java.util.List;

/**
 * Created by liwei on 16/9/21.
 */
public class CommonReplyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mcontent;
    private List<CommonReplyBean> commonReplyBeanList;
    private Handler handler;


    public CommonReplyAdapter(Context context, List<CommonReplyBean> commonReplyBeanList,Handler handler) {
        this.mcontent = context;
        this.commonReplyBeanList = commonReplyBeanList;
        this.handler = handler;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = new ViewHolder(
                LayoutInflater.from(mcontent).inflate(R.layout.commonreply_item, parent, false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder,final int position) {

        ViewHolder viewHolder = (ViewHolder) holder;

        viewHolder.message.setText(commonReplyBeanList.get(position).getMessage());

        //删除item
        viewHolder.llc.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                SwipeLayout.shrinkAllView();
                App.getDBcApplication().deleteFriendCommonReply(commonReplyBeanList.get(position).getId());
                commonReplyBeanList.remove(position);
                notifyItemRemoved(position);
                Message message = new Message();
                message.what = 2;
                handler.sendMessage(message);
                return false;
            }
        });

        viewHolder.message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message message = new Message();
                message.what = 1;
                message.obj = commonReplyBeanList.get(position).getMessage();
                handler.sendMessage(message);
            }
        });

    }

    @Override
    public int getItemCount() {
        return commonReplyBeanList.size();
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        //侧滑布局
        public SwipeLayout swipeLayout;
        TextView message;
        LinearLayout llc;
        public ViewHolder(View itemView) {
            super(itemView);
            message = (TextView)itemView.findViewById(R.id.message);
            swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swip);
            //添加侧滑
            SwipeLayout.addSwipeView(swipeLayout);

            llc = (LinearLayout) itemView.findViewById(R.id.llc);

        }
    }
}
